var g = Object.defineProperty;
var _ = (a, i, n) =>
  i in a
    ? g(a, i, { enumerable: !0, configurable: !0, writable: !0, value: n })
    : (a[i] = n);
var c = (a, i, n) => (_(a, typeof i != 'symbol' ? i + '' : i, n), n),
  I = (a, i, n) => {
    if (!i.has(a)) throw TypeError('Cannot ' + n);
  };
var T = (a, i, n) => {
  if (i.has(a))
    throw TypeError('Cannot add the same private member more than once');
  i instanceof WeakSet ? i.add(a) : i.set(a, n);
};
var u = (a, i, n) => (I(a, i, 'access private method'), n);
import { _Hooks as y } from './Hooks.js';
import { _Lexer as d } from './Lexer.js';
import { _Parser as m } from './Parser.js';
import { _Renderer as z } from './Renderer.js';
import { _TextRenderer as R } from './TextRenderer.js';
import { _Tokenizer as E } from './Tokenizer.js';
import { _getDefaults as H } from './defaults.js';
import { escape as O } from './helpers.js';
import './rules.js';
var p, v, w, P;
class W {
  constructor(...i) {
    T(this, p);
    T(this, w);
    c(this, 'defaults', H());
    c(this, 'options', this.setOptions);
    c(this, 'parse', u(this, p, v).call(this, d.lex, m.parse));
    c(
      this,
      'parseInline',
      u(this, p, v).call(this, d.lexInline, m.parseInline),
    );
    c(this, 'Parser', m);
    c(this, 'Renderer', z);
    c(this, 'TextRenderer', R);
    c(this, 'Lexer', d);
    c(this, 'Tokenizer', E);
    c(this, 'Hooks', y);
    this.use(...i);
  }
  /**
   * Run callback for every token
   */
  walkTokens(i, n) {
    var l, e;
    let s = [];
    for (const o of i)
      switch (((s = s.concat(n.call(this, o))), o.type)) {
        case 'table': {
          const t = o;
          for (const r of t.header) s = s.concat(this.walkTokens(r.tokens, n));
          for (const r of t.rows)
            for (const f of r) s = s.concat(this.walkTokens(f.tokens, n));
          break;
        }
        case 'list': {
          const t = o;
          s = s.concat(this.walkTokens(t.items, n));
          break;
        }
        default: {
          const t = o;
          (e =
            (l = this.defaults.extensions) == null ? void 0 : l.childTokens) !=
            null && e[t.type]
            ? this.defaults.extensions.childTokens[t.type].forEach(r => {
                s = s.concat(this.walkTokens(t[r], n));
              })
            : t.tokens && (s = s.concat(this.walkTokens(t.tokens, n)));
        }
      }
    return s;
  }
  use(...i) {
    const n = this.defaults.extensions || { renderers: {}, childTokens: {} };
    return (
      i.forEach(s => {
        const l = { ...s };
        if (
          ((l.async = this.defaults.async || l.async || !1),
          s.extensions &&
            (s.extensions.forEach(e => {
              if (!e.name) throw new Error('extension name required');
              if ('renderer' in e) {
                const o = n.renderers[e.name];
                o
                  ? (n.renderers[e.name] = function (...t) {
                      let r = e.renderer.apply(this, t);
                      return r === !1 && (r = o.apply(this, t)), r;
                    })
                  : (n.renderers[e.name] = e.renderer);
              }
              if ('tokenizer' in e) {
                if (!e.level || (e.level !== 'block' && e.level !== 'inline'))
                  throw new Error(
                    "extension level must be 'block' or 'inline'",
                  );
                const o = n[e.level];
                o ? o.unshift(e.tokenizer) : (n[e.level] = [e.tokenizer]),
                  e.start &&
                    (e.level === 'block'
                      ? n.startBlock
                        ? n.startBlock.push(e.start)
                        : (n.startBlock = [e.start])
                      : e.level === 'inline' &&
                        (n.startInline
                          ? n.startInline.push(e.start)
                          : (n.startInline = [e.start])));
              }
              'childTokens' in e &&
                e.childTokens &&
                (n.childTokens[e.name] = e.childTokens);
            }),
            (l.extensions = n)),
          s.renderer)
        ) {
          const e = this.defaults.renderer || new z(this.defaults);
          for (const o in s.renderer) {
            if (!(o in e)) throw new Error(`renderer '${o}' does not exist`);
            if (o === 'options') continue;
            const t = o,
              r = s.renderer[t],
              f = e[t];
            e[t] = (...k) => {
              let h = r.apply(e, k);
              return h === !1 && (h = f.apply(e, k)), h || '';
            };
          }
          l.renderer = e;
        }
        if (s.tokenizer) {
          const e = this.defaults.tokenizer || new E(this.defaults);
          for (const o in s.tokenizer) {
            if (!(o in e)) throw new Error(`tokenizer '${o}' does not exist`);
            if (['options', 'rules', 'lexer'].includes(o)) continue;
            const t = o,
              r = s.tokenizer[t],
              f = e[t];
            e[t] = (...k) => {
              let h = r.apply(e, k);
              return h === !1 && (h = f.apply(e, k)), h;
            };
          }
          l.tokenizer = e;
        }
        if (s.hooks) {
          const e = this.defaults.hooks || new y();
          for (const o in s.hooks) {
            if (!(o in e)) throw new Error(`hook '${o}' does not exist`);
            if (o === 'options') continue;
            const t = o,
              r = s.hooks[t],
              f = e[t];
            y.passThroughHooks.has(o)
              ? (e[t] = k => {
                  if (this.defaults.async)
                    return Promise.resolve(r.call(e, k)).then(b =>
                      f.call(e, b),
                    );
                  const h = r.call(e, k);
                  return f.call(e, h);
                })
              : (e[t] = (...k) => {
                  let h = r.apply(e, k);
                  return h === !1 && (h = f.apply(e, k)), h;
                });
          }
          l.hooks = e;
        }
        if (s.walkTokens) {
          const e = this.defaults.walkTokens,
            o = s.walkTokens;
          l.walkTokens = function (t) {
            let r = [];
            return (
              r.push(o.call(this, t)), e && (r = r.concat(e.call(this, t))), r
            );
          };
        }
        this.defaults = { ...this.defaults, ...l };
      }),
      this
    );
  }
  setOptions(i) {
    return (this.defaults = { ...this.defaults, ...i }), this;
  }
  lexer(i, n) {
    return d.lex(i, n ?? this.defaults);
  }
  parser(i, n) {
    return m.parse(i, n ?? this.defaults);
  }
}
(p = new WeakSet()),
  (v = function (i, n) {
    return (s, l) => {
      const e = { ...l },
        o = { ...this.defaults, ...e };
      this.defaults.async === !0 &&
        e.async === !1 &&
        (o.silent ||
          console.warn(
            'marked(): The async option was set to true by an extension. The async: false option sent to parse will be ignored.',
          ),
        (o.async = !0));
      const t = u(this, w, P).call(this, !!o.silent, !!o.async);
      if (typeof s > 'u' || s === null)
        return t(new Error('marked(): input parameter is undefined or null'));
      if (typeof s != 'string')
        return t(
          new Error(
            'marked(): input parameter is of type ' +
              Object.prototype.toString.call(s) +
              ', string expected',
          ),
        );
      if ((o.hooks && (o.hooks.options = o), o.async))
        return Promise.resolve(o.hooks ? o.hooks.preprocess(s) : s)
          .then(r => i(r, o))
          .then(r => (o.hooks ? o.hooks.processAllTokens(r) : r))
          .then(r =>
            o.walkTokens
              ? Promise.all(this.walkTokens(r, o.walkTokens)).then(() => r)
              : r,
          )
          .then(r => n(r, o))
          .then(r => (o.hooks ? o.hooks.postprocess(r) : r))
          .catch(t);
      try {
        o.hooks && (s = o.hooks.preprocess(s));
        let r = i(s, o);
        o.hooks && (r = o.hooks.processAllTokens(r)),
          o.walkTokens && this.walkTokens(r, o.walkTokens);
        let f = n(r, o);
        return o.hooks && (f = o.hooks.postprocess(f)), f;
      } catch (r) {
        return t(r);
      }
    };
  }),
  (w = new WeakSet()),
  (P = function (i, n) {
    return s => {
      if (
        ((s.message += `
Please report this to https://github.com/markedjs/marked.`),
        i)
      ) {
        const l =
          '<p>An error occurred:</p><pre>' + O(s.message + '', !0) + '</pre>';
        return n ? Promise.resolve(l) : l;
      }
      if (n) return Promise.reject(s);
      throw s;
    };
  });
export { W as Marked };
